package ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.base.BaseFragment;
import com.fy.fyzf.bean.AreaListBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.DemandListBean;
import com.fy.fyzf.bean.DemanddetailBean;
import com.fy.fyzf.bean.GetDemandDetailBean;
import com.fy.fyzf.bean.IsInfoBean;
import com.fy.fyzf.bean.MyRecommendBean;
import com.fy.fyzf.glideUtils.GlideUtils;
import com.fy.fyzf.presenter.DemandPresenter;
import com.fy.fyzf.utils.TabLayoutUtils;
import com.fy.fyzf.view.DemandCallBack;
import com.fy.fyzf.view.NoScrollViewPager;
import com.fy.fyzf.weight.MyViewPager;
import com.google.android.material.tabs.TabLayout;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ui.adapter.DemandDetailAdapter;
import ui.fragment.DemandDetailFragment;
import ui.fragment.DemandDetailFragment2;

public class DemandDetailActivity extends BaseActivity<DemandPresenter> implements DemandCallBack {

    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_peer_schme)
    TextView tvPeerSchme;
    @BindView(R.id.flowlayout)
    TagFlowLayout flowlayout;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @BindView(R.id.tv_response_count)
    TextView tvResponseCount;
    @BindView(R.id.tv_close)
    TextView tvClose;
    @BindView(R.id.viewpager)
    NoScrollViewPager viewpager;
    @BindView(R.id.tablayout)
    TabLayout tabLayout;

    private DemandDetailAdapter demandDetailAdapter;
    private DemandListBean.ListBean data;

    @Override
    protected DemandPresenter createPresenter() {
        return new DemandPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_peer_detail;
    }

    private List<BaseFragment> fragmentList = new ArrayList<>();

    @Override
    public void initView() {
        super.initView();
        data = (DemandListBean.ListBean) getIntent().getSerializableExtra("data");

        setTitleName("需求详情");
        int type = getIntent().getIntExtra("type", 0);
        demandDetailAdapter = new DemandDetailAdapter(type);


        fragmentList.add(DemandDetailFragment.newInstance(false, data.getReleaseId()));
        fragmentList.add(DemandDetailFragment2.newInstance(true, data.getReleaseId()));
        tabLayout.addTab(tabLayout.newTab().setText("全部推荐"));
        tabLayout.addTab(tabLayout.newTab().setText("我的推荐"));

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewpager.setCurrentItem(tab.getPosition(), false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        TabLayoutUtils.setTabLayoutWidth(15, 15, tabLayout, this);
        viewpager.setAdapter(new Myadapter(getSupportFragmentManager()));

    }



    @OnClick({R.id.tv_peer_schme, R.id.tv_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_peer_schme:
                break;
            case R.id.tv_close:
                Intent intent = new Intent(DemandDetailActivity.this, AddHousingActivity.class);
                intent.putExtra("type", 2);
                intent.putExtra("releaseId", data.getReleaseId());
                intent.putExtra("phone", data.getUserMobile());
                startActivity(intent);
                break;
        }
    }

    public class Myadapter extends FragmentPagerAdapter {

        public Myadapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    @Override
    public void initData() {
        super.initData();
        showData(data);
        GetDemandDetailBean commonSearchBean = new GetDemandDetailBean();
        commonSearchBean.setReleaseId(data.getReleaseId());
        if (null == data.getIsMy()) {
            commonSearchBean.setIsMy(false);
        } else {
            commonSearchBean.setIsMy(true);
        }
        mPresenter.getDamendDetail(commonSearchBean);
    }

    private void showData(DemandListBean.ListBean data) {
        GlideUtils.loadCircleImage(data.getUserPortrait(), ivHead);
        tvName.setText(data.getUserName());
        List<String> list = new ArrayList<>();
        list.add(data.getAreaName());
        list.add(data.getReleaseArea() + "m²");
        list.add(data.getReleaseBudget() + "元/天");

        switch (data.getRenovationType()) {
            case 0:
                list.add("装修不限");
                break;
            case 1:
                list.add("精装");
                break;
            case 2:
                list.add("简装");
                break;
            case 3:
                list.add("豪装");
                break;
            case 4:
                list.add("无装修");
                break;
        }

        switch (data.getReleaseType()) {
            case 1:
                tvType.setText("写字楼");
                break;
            case 2:
                tvType.setText("商铺");
                break;
            case 3:
                tvType.setText("厂房");
                break;
            case 4:
                tvType.setText("酒店");
                break;
            case 5:
                tvType.setText("公寓");
                break;
        }
        flowlayout.setAdapter(new TagAdapter<String>(list) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(DemandDetailActivity.this).inflate(R.layout.item_peer_tab,
                        flowlayout, false);
                tv.setText(list.get(position));
                return tv;
            }
        });
        tvCompany.setText(data.getCompanyName());

        if (data.getCooperationProgram() != null) {
            switch (data.getCooperationProgram()) {
                case 1:
                    tvPeerSchme.setText("合作方案: 10%");
                    break;
                case 2:
                    tvPeerSchme.setText("合作方案: 20%");
                    break;
                case 3:
                    tvPeerSchme.setText("合作方案: 30%");
                    break;
                case 4:
                    tvPeerSchme.setText("合作方案: 40%");
                    break;
                case 5:
                    tvPeerSchme.setText("合作方案: 50%");
                    break;
            }
        }

        if (data.getOtherNeeds() != null) {
            tvRemark.setText(data.getOtherNeeds());
        } else {
            tvRemark.setText("暂无其他要求描述，详情请去电咨询。");
        }

        tvResponseCount.setText("已收到" + data.getResponseCount() + "条推荐");
    }

    @Override
    public void getDemandListSuccess(DemandListBean demandListBean) {

    }

    @Override
    public void issueDemandSuccess(BaseData data) {

    }

    @Override
    public void getAreaListSuccess(List<AreaListBean> listBeans) {

    }

    @Override
    public void getDamendDetailSuccess(DemanddetailBean demanddetailBean) {
//        demandDetailAdapter.setNewData(demanddetailBean.getPublisherDetailsReleaseVo().getList());
    }

    @Override
    public void getMyDemandListSuccess(DemandListBean demandListBean) {

    }

    @Override
    public void deleteDemandSuccess(BaseData data, int i) {

    }

    @Override
    public void getMyRecommendSuccess(MyRecommendBean myRecommendBean) {

    }

    @Override
    public void isInfoSuccess(IsInfoBean isInfoBean) {

    }


}